package com.ls.controller;

import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.ls.model.User;

@Controller
public class HelloController {

	@RequestMapping("/")
	public String hello(@RequestParam("name") String userName, Model model) {
		model.addAttribute("message", "Hello From the Controller, " + userName);
		return "resultPage";
	}
	
	/*@Secured(value = "ROLE_USER")*/
	@RequestMapping("/index")
	public String index() {
		return "index";
	}
	
	/*@Secured("ROLE_ADMIN")*/
	@RequestMapping("/form")
	public String form(User user) {
		return "form";
	}
	
	@RequestMapping(value = "/form", method = RequestMethod.POST)
	public String saveForm(@Valid User user, BindingResult bindingResult) {
		if (bindingResult.hasErrors()) {
			return "form";
		}
		System.out.println("Save OK,{}" + user.toString());
		return "index";
	}
	
	@RequestMapping("/login")
	public String loginPage() {
		return "login";
	}
	
}
